import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:old_shop/common/const/cosnt.dart';
import 'package:old_shop/login&regist/view/sms_input.dart';
import 'package:old_shop/login&regist/view/sms_view.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SmsCodePage extends StatelessWidget {
  final Map params;

  const SmsCodePage({Key? key, required this.params}) : super(key: key);

  String _phone() {
    final phone = params["phone"].toString();

    return phone.substring(0, 3) + " " + phone.substring(3, 7) + " " + phone.substring(7, phone.length);
  }

  void _jumpSettingpwd(BuildContext context, String code) {
    if (code.length == 6) {
      params["code"] = code;
      _userisRegist(context);
    }
  }

  void _jumpToRoot(BuildContext context) {
    SharedPreferences.getInstance().then((value) => value.setBool(AppConst.APP_IS_LOGIN, true));
    Navigator.of(context).pushNamedAndRemoveUntil('/root', (Route<dynamic> route) => false);
  }

  void _userisRegist(BuildContext context) async {
    EasyLoading.show(status: "Loading...");
    EasyLoading.dismiss();
    await Future.delayed(const Duration(seconds: 1));
    _jumpToRoot(context);
  }

  void _regist(BuildContext context) {
    EasyLoading.show(status: "Loading...");
    // 注册或者登录
    if (params['forget'] == true) {
      // 老用户忘记密码了.
      Navigator.pushNamed(context, "/resetpwd");
    } else {
      _jumpToRoot(context);
    }
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    final marginH = size.height * 0.06;

    return Scaffold(
      body: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            IconButton(
              icon: Icon(Icons.arrow_back_ios),
              onPressed: () => Navigator.pop(context),
            ),
            SizedBox(height: marginH),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 15),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    "请输入验证码",
                    style: AppFont.textStyle(26, color: Colors.black, fontWeight: FontWeight.bold),
                  ),
                  SizedBox(height: 8),
                  Text.rich(TextSpan(children: [
                    TextSpan(text: '验证码已发送至', style: AppFont.textStyle(13, color: AppColor.fontgrey)),
                    TextSpan(text: ' ' + _phone(), style: AppFont.textStyle(13, color: AppColor.font333))
                  ])),
                  Padding(
                      padding: EdgeInsets.only(top: marginH, bottom: 10),
                      child: SmsInputView(
                        onChanged: (val) {
                          _jumpSettingpwd(context, val);
                        },
                      )),
                  SmsCounterView(
                    phone: params["phone"],
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
